<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_ticketcontrol extends base {

    function admin_ticketcontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('ticket');
        $this->load('views');
        $this->load('setting');
    }

    function ondefault($msg='') {
        $msg && $message=$msg;
        @$page = max(1, intval($this->get[2]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $ticketlist = $_ENV['ticket']->get_list($startindex,$pagesize);
        $ticketnum=$this->db->fetch_total('ticket');
        $departstr=page($ticketnum, $pagesize, $page,"admin_ticket/default");
        $ticket_range = unserialize($this->setting['ticket_range']);
        include template('ticketloglist','admin');
    }
     function onviewlist($msg='') {
        $msg && $message=$msg;
        @$page = max(1, intval($this->get[2]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $ticketlist = $_ENV['ticket']->get_list($startindex,$pagesize);
        $ticketnum=$this->db->fetch_total('ticket');
        $departstr=page($ticketnum, $pagesize, $page,"admin_ticket/default");
        $ticket_range = unserialize($this->setting['ticket_range']);
        include template('ticketlist','admin');
    }
	function onaddone() {
        if(isset($this->post['sid'])) {
            $name = $this->post['tname'];
            $price = $this->post['price'];
            $mprice = $this->post['mprice'];
            $sid = intval($this->post['sid']);
            $page = intval($this->post['page']);
            if(''==$name || !$price) {
                msg('请正确填写门票相关信息！',"index.php?admin_views/default/{$page}");
                exit;
            }else{
            	$_ENV['ticket']->add($name,$price,$mprice,$sid);
            	msg('添加成功！',"index.php?admin_views/default/{$page}");
                exit;
            }
            
        }
    }
    function oneditone() {
        if(isset($this->post['sid'])) {
            $name = $this->post['tname'];
            $price = $this->post['tprice'];
            $mprice = $this->post['mprice'];
            $sid = intval($this->post['sid']);
            $page = intval($this->post['page']);
            if(''==$name || !$price) {
                msg('请正确填写门票相关信息！',"index.php?admin_views/default/{$page}");
                exit;
            }else{
            	$_ENV['ticket']->edit($name,$price,$mprice,$sid);
            	msg('编辑成功！',"index.php?admin_views/default/{$page}");
                exit;
            }
            
        }
    }
    function ondelone(){
    	$page = intval($this->get[3]);
    	$aid = intval($this->get[2]);
    	$_ENV['ticket']->delone($aid);
    	msg('删除成功！',"index.php?admin_views/default/{$page}");
    }
    function onadd() {
        if(isset($this->post['submit'])) {
            $title = $this->post['ticketname'];
            $desrc = $this->post['ticketdesrc'];
            $credit = intval($this->post['ticketprice']);
            $imgname=strtolower($_FILES['imgurl']['name']);
            if(''==$title || !$credit) {
                $this->ondefault('请正确填写门票相关信息！');
                exit;
            }
            $type = substr(strrchr($imgname,'.'),1);
            if(!isimage($type)) {
                $this->ondefault('图片格式不支持，目前仅支持jpg、gif、png格式！');
                exit;
            }

            
            $filepath = '/data/attach/ticketimg/gfit'.random(6,0).'.'.$type;
            forcemkdir(MY_ROOT.'/data/attach/ticketimg');
            if(move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath)) {
                $_ENV['ticket']->add($title,$desrc,$filepath,$credit);
                $this->ondefault('添加成功！');
            }else {
                $this->ondefault('服务器忙，请稍后再试！');
            }
        }else {
            include template('addticket','admin');
        }
    }

    function onedit() {
        $gid = intval($this->get[2])?$this->get[2]:$this->post['id'];
        if(isset($this->post['submit'])) {
            $title = $this->post['ticketname'];
            $desrc = $this->post['ticketdesrc'];
            $credit = intval($this->post['ticketprice']);
            $imgname=strtolower($_FILES['imgurl']['name']);
            if(''==$title || !$credit) {
                $message='请正确填写门票相关信息';
                $type='errormsg';
                include template('addticket','admin');
                exit;

            }

            $type = substr(strrchr($imgname,'.'),1);
            if(!empty($_FILES['imgurl']['tmp_name']) && (!isimage($type))) {
                $message='图片格式不支持，目前仅支持jpg、gif、png格式！';
                $type='errormsg';
                include template('addticket','admin');
                exit;
            }


            $filepath = '/data/attach/gfit'.random(6,0).'.'.$type;
            forcemkdir(MY_ROOT.'/data/attach/ticketimg');
            if(!empty($_FILES['imgurl']['tmp_name']) && (!move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath))) {
                $message='服务器忙，请稍后再试！';
                $type='errormsg';
                include template('addticket','admin');
                exit;

            }
            empty($_FILES['imgurl']['tmp_name']) && $filepath =$this->post['imgpath'];


            $_ENV['ticket']->update($title,$desrc,$filepath,$credit,$gid);
            $message = "修改成功!";
        }

        $ticket= $_ENV['ticket']->get($gid);
        include template('addticket','admin');
    }

    function onaddrange() {
        $rangelist = unserialize($this->setting['ticket_range']);
        if(isset($this->post['submit'])) {
            $ranges = $this->post['ticket_range'];
            $rangesize = count($ranges);
            $ticketrange = array();
            for($i=0;$i<$rangesize;$i++) {
                if($i>0 && $ranges[$i]!= '' && $ranges[$i]%2 == 0)
                    $ticketrange[$ranges[$i-1]]=$ranges[$i];
            }
            if((count($ticketrange)%2) != 0) {
                $message = '请正确填写价格范围，必须为整数对！';
                $type= 'errormsg';
                include template('ticketrange','admin');
                exit;
            }
            $rangelist = $ticketrange;
            $this->setting['ticket_range'] = serialize($ticketrange);
            $_ENV['setting']->update($this->setting);
            $message = '设置成功！';
        }
        include template('ticketrange','admin');
    }

    function onnote() {
        if(isset($this->post['submit'])) {
            $this->setting['ticket_note'] = $this->post['note'];
            $_ENV['setting']->update($this->setting);
            $message = '设置公告成功！';
        }
        include template('ticketnote','admin');

    }

    function onremove() {
        $message='没有选择门票！';
        if(isset($this->post['gid'])) {
            $gids = implode(",", $this->post['gid']);
            $_ENV['ticket']->remove_by_id($gids);
            $message='门票删除成功！';
            unset($this->get);
        }
        $this->ondefault($message);
    }

    function onavailable() {
        if(isset($this->post['gid'])) {
            $gids = implode(",", $this->post['gid']);
            $_ENV['ticket']->update_available($gids,$this->get[2]);
            $message = $this->get[2] ? '门票设为可用成功!':'门票设置过期成功!';
            unset($this->get);
            $this->ondefault($message);

        }
    }

    function onlog($msg='') {
        @$page = max(1, intval($this->get[2]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $loglist = $_ENV['ticket']->getlog($startindex,$pagesize);

        $ticketlognum=$this->db->fetch_total('ticketlog');
        $departstr=page($ticketnum, $pagesize, $page,"admin_ticket/log");
        $msg && $message=$msg;
        $ticket_range = unserialize($this->setting['ticket_range']);
        include template("ticketloglist",'admin');
    }
     function onsend() {
        if(isset($this->post['id'])) {
            $ids = implode(",", $this->post['id']);
            $_ENV['ticket']->update_ticket_status($ids,$this->get[2]);
            $message = '门票成功设置为已评论！';
            unset($this->get);
            $this->onlog($message);
        }
    }

    function onsearch(){
        
        @$page = max(1, intval($this->get[4]));
        $range = isset($this->post['pricerange'])?$this->post['pricerange']:$this->get[2];
        $ticketname =isset($this->post['ticketname'])?$this->post['ticketname']:$this->get[3];
        $pagesize= 1;
        $startindex = ($page - 1) * $pagesize;
        $rangesql = '';
        $ranges = explode("-",$range);
        $ticketlist = $_ENV['ticket']->get_by_range_name($ranges,$ticketname,$startindex,$pagesize);
        (count($ranges)>1) && $rangesql= "AND `credit`>=$ranges[0] AND `credit`<=$ranges[1]";
        $rownum=$this->db->fetch_total('ticket'," `title` LIKE '$ticketname%' $rangesql");
        $departstr=page($rownum, $pagesize, $page,"admin_ticket/search/$range/$ticketname");
        $ticket_range = unserialize($this->setting['ticket_range']);
        include template('ticketlist','admin');
    }

    function onlogsearch(){

        $pricerange = isset($this->get[2])?$this->get[2]:$this->post['pricerange'];
        $ticketname = isset($this->get[3])?$this->get[3]:$this->post['ticketname'];
        $username = isset($this->get[4])?$this->get[4]:$this->post['username'];
        $datestart = isset($this->get[5])?$this->get[5]:$this->post['srchregdatestart'];
        $dateend = isset($this->get[6])?$this->get[6]:$this->post['srchregdateend'];
        @$page = max(1, intval($this->get[7]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $loglist = $_ENV['ticket']->list_by_searchlog($pricerange,$ticketname,$username,$datestart,$dateend,$startindex,$pagesize);
        $ticketlognum = $_ENV['ticket']->rownum_by_searchlog($pricerange,$ticketname,$username,$datestart,$dateend);
        $departstr=page($ticketlognum, $pagesize, $page,"admin_ticket/logsearch/$pricerange/$ticketname/$username/$datestart/$dateend");
        $ticket_range = unserialize($this->setting['ticket_range']);
        include template("ticketloglist",'admin');
    }

}

?>